package com.zj.common.exception;

import com.zj.common.ResultEnum;
import com.zj.common.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author zhangbo
 * @program: PDF-helper
 * @ClassName ExceptionAdvise
 * @description: $
 * @date 2023-05-22 22:37:42
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {


    /**
     * 通用异常
     * @param ex 异常
     * @return 结果
     */
    @ExceptionHandler(value = CommonException.class)
    public ResultVo commonException(CommonException ex) {
        logError(CommonException.class, ResultEnum.ERROR, ex);
        return ResultVo.error("系统异常，请稍后重试");
    }


    /**
     * 默认异常
     * @param ex 异常
     * @return 结果
     */
    @ExceptionHandler(value = RuntimeException.class)
    public ResultVo runTimeException(RuntimeException ex){
        logError(RuntimeException.class, ResultEnum.ERROR, ex);
        return ResultVo.error("系统异常，请稍后重试");
    }


    public void logError(Class errorType, Enum errorEnum, Throwable throwable) {
        log.error("[{}] {}: {}", errorType.getSimpleName(), errorEnum, throwable.getMessage(), throwable);
    }


}
